﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class FangXing_DAL
    {
        public FangXing_DAL(Context context)
        {
            Context = context;
        }

        public Context Context { get; }



        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int FangXingAdd(FangXing_Model m)
        {
            var m1=Context.FangXing_Model.FirstOrDefault(x=>x.FXName == m.FXName);
            if(m1 != null)
            {
                return -1;
            }
            else
            {
                Context.FangXing_Model.Add(m);
                return Context .SaveChanges();
            }
        }
        /// <summary>
        /// 房型列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="FXName">房型名称</param>
        /// <param name="FXMJ">房型面积</param>
        /// <param name="Count"></param>
        /// <param name="Count2"></param>
        /// <returns></returns>
        public List<FangXing_Model> FangXingShow(int index, int size, string? FXName, string? FXMJ, out int Count, out int Count2)
        {
            var m = Context.FangXing_Model.AsQueryable().ToList();
            if (!string.IsNullOrEmpty(FXName))
            {
                m = m.Where(x => x.FXName.Contains(FXName)).ToList();

            }
            if (!string.IsNullOrEmpty(FXMJ))
            {
                m = m.Where(x => x.FXMJ == FXMJ).ToList();
            }
            Count = m.Count;
            Count2 = (int)Math.Ceiling(Count * 1.0 / size);
            return m.OrderBy(x=>x.FXId).Skip((index-1)*size).Take(size).ToList();

        }

        /// <summary>
        /// 房型删除
        /// </summary>
        /// <param name="FXId"></param>
        /// <returns></returns>
        public int FangXingShan(int FXId)
        {
           var m=Context.FangXing_Model.FirstOrDefault(x=>x.FXId == FXId);
            m.FXIsDeleted = "1";
            return Context.SaveChanges();

        }


    }
}
